const md = require('../../utils/md5.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputData: {
      input_value: "",//输入框的初始内容
      value_length: 0,//输入框密码位数
      isNext: true,//是否有下一步的按钮
      get_focus: true,//输入框的聚焦状态
      focus_class: true,//输入框聚焦样式
      value_num: [1, 2, 3, 4],//输入框格子数
      height: "80rpx",//输入框高度
      width: "400rpx",//输入框宽度
      see: true,//是否明文展示
      interval: false,//是否显示间隔格子
      maxlength: true
    },
    val_arr: [],
    input_value: '',
    msg: '获取',
    num:2,
    count:60
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that =this;
    that.setData({
      userinfo: wx.getStorageSync('userinfo')
    });
    var phone =that.data.userinfo.Tel;
    phone = phone.substr(phone.length - 4);
    console.log(options);
    that.setData({
      phone:phone,
      pwd:options.pwd
    })
    this.getmsg();
  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  values(e) {
    var that = this;
    that.setData({
      msg: e.detail
    })
    console.log(e);
  },
  getmsg: function () {
    var that = this
    // console.log(1);
    this.setData({
      num: that.data.num + 1
    })
    console.log(that.data.num)
    if (that.data.num == 2) {
      // 获取时间戳
      var apinameIsMeb = "/Action/PK01/SMSVerifySend";
      var timestamp = new Date().getTime();
      timestamp = parseInt(timestamp / 1000);
      var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
      wx.request({
        //后台接口地址
        url: app.server.url + apinameIsMeb,
        data: {
          sign: md5IsMeb,
          appid: app.server.appid,
          tmsp: timestamp,
          mobile: that.data.userinfo.Tel
        },
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {
          console.log(res);
          if(res.data.code == 0){
            wx.showToast({
              title: '短信发送成功',
              icon: 'success',
              duration: 2000
            })
          } else {
            wx.showModal({
              title: '',
              content: res.data.msg,
            })
          }
        }
      })
    }
    if (that.data.count == 0) {
      this.setData({
        num: 1
      })
      return;
    } else {
      var interval = setInterval(function () {
        if (that.data.count == 0) {
          clearInterval(interval);
          that.setData({
            msg: '重新获取',
            count: 60,
            num:1
          })
          return true;
        }
        that.setData({
          msg: that.data.count - 1 + 's',
          count: that.data.count - 1
        })
      }, 1000)
    }
  },
  formSubmit: function (e) {
    console.log(e.detail.value);
    var that = this;
    var pwd = that.data.pwd;
    var msg = e.detail.value.msg;
    // return;
    if (msg.length != 4) {
      wx.showModal({
        title: '',
        content: '请输入正确位数的验证码',
      })
      return;
    }
    // 验证短信
    // 获取时间戳
    var apinameIsMeb = "/Action/PK01/SMSVerifyCheck";
    var timestamp = new Date().getTime();
    timestamp = parseInt(timestamp / 1000);
    var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
    wx.request({
      //后台接口地址
      url: app.server.url + apinameIsMeb,
      data: {
        sign: md5IsMeb,
        appid: app.server.appid,
        tmsp: timestamp,
        mobile: that.data.userinfo.Tel,
        code: msg
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        console.log(res);
        if (res.data.data == true) {
          var info = e.detail.value;
          // 获取时间戳
          var apinameIsMeb = "/BO/BA_Member/Modify";
          var timestamp = new Date().getTime();
          timestamp = parseInt(timestamp / 1000);
          var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
          var data = {};
          data.MebID = (wx.getStorageSync('userinfo')).MebID;
          data.PayPass = md.hexMD5(pwd);
          data = JSON.stringify(data);
          wx.request({
            //后台接口地址
            url: app.server.url + apinameIsMeb,
            data: {
              sign: md5IsMeb,
              appid: app.server.appid,
              tmsp: timestamp,
              data: data
            },
            method: 'POST',
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            success: function (res) {
              if (res.data.code == 0) {
                wx.switchTab({
                  url: '/pages/mine/mine',
                })
              }
            }
          })
          // console.log('success');
        } else {
          wx.showModal({
            title: '',
            content: '验证码错误，请检查重试~',
          })
          return;
        }
      }
    })
  },
})